<template>
    <div class="xcontainer">
      <div class="xheader">
        <div class="topBg"></div>
        <img src="../../assets/img/jsszy/logo.png" alt="" class="logo">
        <img src="../../assets/img/jsszy/monted.png" alt="" class="mountian">
      </div>
      <div class="xcontent flex_full">
        <img src="../../assets/img/jsszy/register_bg.png" alt="">
        <div class="xbox">
          <p class="flex flex_vcenter pdb20">
            <i class="titleTag"></i>
            <span class="fRedBig">请选择角色</span>
          </p>
          <ul>
            <li v-for="item in role_list" class="pd10">
              <span>{{item.roleName}}</span>
              <input :value="item" v-model="selRole" type="radio"/>
            </li>
          </ul>
          <p class="longBtn mt20" v-show="showNext" @click="toMb()">点击进入</p>
        </div>
      </div>
    </div>
</template>

<script>
  var md5 = require("md5");
  import 'assets/lib/bootstrap-3.3.2/css/bootstrap.min.css'
  import 'assets/lib/bootstrap-3.3.2/js/bootstrap.min.js'
  import 'assets/reset.css'
  import 'assets/flex.css'
  import Public from 'assets/public.js'
  import moduleHead from 'components/module-head'
  import menuList from 'components/menu-list'
  import bootPage from 'components/BootPage.vue'
  export default {
    data () {
      return {
        role_list: [],
        showNext: false,
        selRole: '',
        userPwd: '',
        userPhone: ''
      }
    },
    ready () {
      // 获取url参数
      var url = window.location.href.split('?')[1]
      var param1 = url.split('&')[0]
      this.userPwd = decodeURIComponent(param1.split('=')[1])
      sessionStorage.setItem('userPwd', this.userPwd)
      var param2 = url.split('&')[1]
      this.userPhone = param2.split('=')[1]
      var param3 = url.split('&')[2]
      var sfToken = param3.split('=')[1]
      var param4 = url.split('&')[3]
      var docDept = param4.split('=')[1]
      sessionStorage.setItem('sfToken', sfToken)
      sessionStorage.setItem('docDept', docDept)
      var self = this
      $.ajax({
        url: Public.ROLES_URL(),
        type: "POST",
        dataType: "json",
        data: JSON.stringify({
          loginName:self.userPhone,
          pwd: md5(self.userPwd),
          forAccessToken: true
        }),
        success: function(res) {
          if (res.code == 200) {
            var data = res.body;
            self.role_list = data.filter(function(item) {
              return item.roleId != "patient";
            });
            if (self.role_list.length == 1) { //如果只有一种角色
              $.ajax({
                url: Public.LOGIN_URL(),
                type: "POST",
                dataType: "json",
                data: JSON.stringify({
                  tenantId: 'hcn.jsszyy',
                  loginName: self.userPhone,
                  rid:res.body[0].roleId,
                  pwd: md5(self.userPwd),
                  forAccessToken: true
                }),
                success: function(res) {
                  sessionStorage.setItem('accessToken', res.properties.accessToken);
                  sessionStorage.setItem('roleId', res.body.roleId);
                  sessionStorage.setItem('userName', res.body.userName);
                  sessionStorage.setItem('userId', res.body.id)
                  self.showNext = true
                }
              });
            } else if (self.role_list.length == 0) {
              Public.ajaxPrompt("账号角色无权限访问")
            } else {
              console.log('multiRole')
              console.log(JSON.stringify(res))
            }
          } else {
            Public.ajaxPrompt("账号角色无权限访问")
          }
        }
      })
    },
    watch: {
      'selRole' (val, oldval) {
        if (val) {
          var self = this
          $.ajax({
            url: Public.LOGIN_URL(),
            type: "POST",
            dataType: "json",
            data: JSON.stringify({
              tenantId: 'hcn.jsszyy',
              loginName: self.userPhone,
              rid:val.roleId,
              pwd: md5(self.userPwd),
              forAccessToken: true
            }),
            success: function(res) {
              sessionStorage.setItem('accessToken', res.properties.accessToken);
              sessionStorage.setItem('userName', res.body.userName);
              sessionStorage.setItem('roleId', res.body.roleId);
              sessionStorage.setItem('userId', res.body.id);
              self.showNext = true
            }
          })
        }
      }
    },
    methods: {
      toMb(){
        window.location.replace("index.html");
      }
    }
  }
</script>

<style scoped>
.xcontainer{
  height:100%;
  display:flex;
  flex-direction: column;
}
  .xbox{
    background-color: white;
    border-radius: 10px;
    padding:20px 40px 20px 20px;
    border:2px solid #EDC1B6;
    box-shadow: 5px 5px 5px #EDC1B6;
  }
  .xcontent{
    display:flex;
    -webkit-display: flex;
    align-items: center;
    -webkit-align-items: center;
    justify-content: center;
    -webkit-justify-content: center;
  }
.topBg{
  height:18px;
  background-image:url('../../assets/img/jsszy/wall_bg.png')
}
  .xheader{
    position: relative;
  }
.xheader>.logo{
  position:absolute;
  top:40px;
  left:100px;
}
  .xheader>.mountian{
    position:absolute;
    top:40px;
    right:100px;
  }
</style>
